<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include '../connection/connection.php';
include "../value/value_kr.php";

$winH = $_REQUEST['winH'];

	$list=$_SESSION['sort'];
	$str=$_GET['string'];
	$_SESSION['sortn']=$str;
	$flagDesc=$_GET['flagDesc'];
	
	
	if(!isset($_REQUEST['pages'])){
		$list["fa.camera_ID"]="DESC";
		$list["fa.pos"]="DESC";
		$list["confc.name"]="DESC";
		
		$str="fa.pos";
		$_SESSION['sortn']=$str;
		$_SESSION['sort']=$list;
	}else{
		$str=$_SESSION['sortn'];
		$list=$_SESSION['sort'];
	}
	if($flagDesc==1){

		if($list[$str]=="DESC")
			$list[$str]="ASC";
		else
			$list[$str]="DESC";
	}
	

$monitorid = $_REQUEST['monitorid'];

$sql = "SELECT fa.camera_ID,fa.pos,confc.`name` FROM monitor mn, favourite fa,
		 cardisplay cardis,configcamera confc WHERE mn.Monitor_ID = fa.monitor_ID AND cardis.id = confc.id 
		AND fa.camera_ID = cardis.id AND mn.Monitor_ID = $monitorid ";
$selectMonitor = mysql_query($sql." ORDER BY ".$str." ".$list[$str],$connection);

$_SESSION['sort']=$list;
	

$selectallmonitor = mysql_query("SELECT mn.Name,mn.Monitor_ID FROM monitor mn order by Pos",$connection);

$selecDetailtMonitor = mysql_query("SELECT mn.`Name`,mn.Pos,mn.timeOut,mn.nCamera,mn.Host FROM monitor mn
		WHERE mn.Monitor_ID = $monitorid GROUP BY mn.`Name` ",$connection);

$row = mysql_fetch_assoc($selecDetailtMonitor);

$selectpostmonitor = mysql_query("SELECT adm.value_name FROM adjustment_monitor adm",$connection);
?>
<table cellpadding="0" cellspacing="0" border="0" width="80%" class="tbm1"  id="detailmonitor">
                <tr>
                    <td><?=$l_monitorname ?></td>
                    <td><input type="text" class="intext" style="width:270px;text-align: left;padding-left: 5px;line-height: 26px;" value="<?=$row['Name']?>"  id="monitorname" name="monitorname"/></td>
                    <td><input type="button" class="inbtn" value="<?=$l_new ?>" style="width: 65px;"   id="newmonitor" name="newmonitor"  onclick="create_monitor();"/></td>
                </tr>
                <tr>
                    <td><?=$l_pos ?></td>
                  <td><input type="text" class="intext" style="width:80px;text-align: left;padding-left: 5px;line-height: 26px;"  value="<?=$row['Pos']?>"  id="monitorpos" name="monitorpos" 
                  	onkeypress="return validate(event);"/></td>
                    <td><input type="button" class="inbtn"  style="width: 65px;" value="<?=$l_edit ?>"  id="editmonitor" name="editmonitor" 
                    onclick="edit_monitor();"/></td>
                </tr>
                <tr>
                    <td><?=$l_timeout ?></td>
                    <td><input type="text" class="intext" style="width:100px;text-align: left;padding-left: 5px;line-height: 26px;"  value="<?=$row['timeOut']?>"  id="timeout" name="timeout" 
                    	onkeypress="return validate(event);"/>
                  		    <?=$l_rateMonitor ?> 
                        <select style="width:70px" class="seltop"  size="1" id="optionPos" name="optionPos">
                        <?php while ($rowpost = mysql_fetch_array($selectpostmonitor)){?>
                      	  <option value="<?=$rowpost[value_name]?>" id="<?=$rowpost['value_name']?>" <? if ($rowpost['value_name']==$row['nCamera']){?> selected="selected" <?php }?> >
                      	  <?=(($rowpost[value_name]+1)*($rowpost[value_name]+1))?></option>
                        <?php }?>
                        </select>
                      </td>
                    <td><input type="button" class="inbtn" style="width: 65px;"  value="<?=$l_delete ?>"  name="deletemonitor" id="deletemonitor" 
                 	   onclick="if(confirm('<?=$l_areyousure?>')){ delete_monitor();return true;}else{return false;}" /></td>
                </tr>
                <tr>
                	<td><?=$l_server?></td>
                	<td><input type="text" class="intext" style="width:270px;padding-left: 5px;line-height: 26px;text-align: left;" id="txthost" name="txthost" value="<?=$row['Host']?>"/></td>
                	<td><input type="button" class="inbtn" style="width: 65px;"  value="<?=$l_reset ?>" onclick="reset_1_Monitor();" id="resetmonitor" name="resetmonitor"/></td>
                </tr>
                <tr>
                    <td><?=$l_monitor ?></td>
                    <td>
                    	<select  class="seltop" style="vertical-align:middle;width: 270px;" size="1" name="monitorid" id="monitorid" onchange="loadCameraById();">
							<?php while ($rowmonitor = mysql_fetch_array($selectallmonitor)){?>
							<option value="<?=$rowmonitor[Monitor_ID]?>" <? if ($rowmonitor[Monitor_ID]==$monitorid){?> selected="selected"<?php }?> ><?php echo $rowmonitor[Name]?></option>
							<?php }?>
						</select>
                    </td>
                </tr>
            </table>
            <div  id="idtablecamera" >
            <form id="mpform" name="mpform">
				<div style="color: white; margin-top: 20px;cursor: default;">
					<input type="checkbox" id="checkall" name="checkall"  onclick="checkSelectAll();"/><?=$l_selectall ?>
				</div>
				
				<div class="divtheader">
	            	<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablebody2left">
		            	<thead>
		                    <tr>
		                    <th style="width: 8%;"><input type="button" class="indelete" value="<?=$l_delete ?>" name="deleteCamera" onclick="if(confirm('<?=$l_areyousure?>')){ processdeleteCamera(<?=$monitorid?>);return true;}else{return false;}" /></th>
							<th style="width: 8%;">No</th>
							<th style="width: 18%"><a onclick="sortAndPagingLeftMonitor('fa.camera_ID',1,<?=$monitorid?>)" style="cursor: pointer;color: #027acc"><?=$l_cameraid ?></a></th>
							<th style="width: 46%"><a onclick="sortAndPagingLeftMonitor('confc.name',1,<?=$monitorid?>)" style="cursor: pointer;color: #027acc"><?=$l_cameraname ?></a></th>
							<th style="width: 10%"><a onclick="sortAndPagingLeftMonitor('fa.pos',1,<?=$monitorid?>)" style="cursor: pointer;color: #027acc"><?=$l_pos ?></a></th>
							<th class="last" style="width: 10%;border-right: none !important;">&nbsp;</th>               
		                </tr>
		                </thead>
	            	</table>
            	</div>
			<div id="table_mo_camera_left">
			<div id="divscrollLeft" style="overflow:scroll;overflow-x: hidden; width:100%; height:<?php echo ($winH-360)."px"?>; background-color: #ffffff">
	 			<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablebodyleft">
	                 <tbody>
	                	<?php
	                		$i = 0; 
	                		while ($row = mysql_fetch_array($selectMonitor)){ 
	                			$i ++;?>
						<tr onclick="onclickrecordcheck('checkboxcamera<?=$i?>')">
							<td style="width: 8%;text-align: center;">
								<input onclick="onclickrecordcheck('checkboxcamera<?=$i?>')" type="checkbox" name="checkboxcamera[]" id="checkboxcamera<?=$i?>"  value="<?=$row[camera_ID]?>"  class="checkCamera"/></td>
							<td style="width: 8%;text-align: center;"><?=$i?></td>
							<td style="width: 18%;text-align: center;"><?=$row[camera_ID]?></td>
							<td style="width: 46%"><?=$row[name]?></td>
							<td style="width: 10%;text-align: center;"><?=$row[pos]?></td>
							<td class="last" style="width: 10%;border-right: none !important;text-align: center;"><a  href="#?w=500" onclick="loadEditMonitor_Popup('<?=$i?>','<?=$row[camera_ID]?>','<?=$row[name]?>','<?=$row[pos]?>',this);"
													rel="popup_name2" class="poplight"><?php echo $l_edit?></a></td>
						</tr>
						<?php
						}
						if ($i>0 and $i<($winH/28)) {
							 	foreach(range(0,intval($winH/28)-$i) as $real){
							 		?>
							 		<tr>
							 			<td style="width: 8%;text-align: center;">&nbsp;</td>
									 	<td  style="width: 8%;text-align: center;">&nbsp;</td>
									 	<td style="width: 18%;text-align: center;">&nbsp;</td>
									 	<td  style="width: 46%;text-align: center;">&nbsp;</td>
									 	<td  style="width: 10%;text-align: center;">&nbsp;</td>
									 	<td  class="last" style="width: 10%;border-right: none !important;">&nbsp;</td>
								 	</tr>
							 	<?php }
							}?>
						
						</tbody>
	                </table>
	                </div>
            </div>
            
            <input type="hidden" id="idmonitor" name="idmonitor" value="<?=$monitorid?>">
            	<input type="hidden" id="pagehidden" name="pagehidden" value="" />
   </form>
            </div>

<div id="popup_name2" class="popup_block">

		<span
			style="background: #288CD3; color: #FFFFFF;font-weight:bold; display: block; padding: 5px 0 5px 10px;"><?php echo "Edit Monitor";?>
		</span>
		<table width="100%" style="margin: 20px 0">
			<tr>
				<td width="35%" style="padding-left: 5px;"><b><?php echo "No";?> </b></td>
				<td valign="top">
					<input type="text" name="txtno" id="txtno" style="height: 23px; width: 95%; border: 1px solid #ccc;margin-bottom:10px;background: #E5E5E5;" readonly="readonly" size="35"/>
				</td>
			</tr>
			<tr>
				<td width="35%" style="padding-left: 5px;"><b><?php echo $l_cameraid;?> </b></td>
				<td valign="top">
					<input type="text" name="txtcameraid" id="txtcameraid" style="height: 23px; width: 95%; border: 1px solid #ccc;margin-bottom:10px;background: #E5E5E5;" readonly="readonly" size="35"/>
				</td>
			</tr>
			<tr>
				<td width="35%" style="padding-left: 5px;"><b><?php echo $l_cameraname;?> </b></td>
				<td valign="top">
					<input type="text" name="txtcameraname" id="txtcameraname" style="height: 23px; width: 95%; border: 1px solid #ccc;margin-bottom:10px;background: #E5E5E5;" readonly="readonly" size="35"/>
				</td>
			</tr>
			<tr>
				<td width="35%" style="padding-left: 5px;"><b><?php echo $l_pos;?> </b></td>
				<td valign="top">
					<input type="text" name="txtposition" id="txtposition" 
					onkeypress="return validate(event);"
					style="height: 23px; width: 95%; border: 1px solid #ccc;margin-bottom:10px;" size="35"/>
				</td>
			</tr>
			
			<tr>
				<td></td>
				<td>
					<div align="center" style="padding-top: 3px;">
						<input type="button" class="inbtn iclose"
							style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; width: 87px; float: right; margin-right: 142px;"
							value="<?=$l_cancel ?>" /> 
						<input type="button" style="float: left; font-family: Arial, Helvetica, sans-serif; font-size: 12px; width: 87px; margin-left: 0px;"
							class="inbtn" value="<?=$l_save ?>" onclick="processSaveEditMonitor_Popup();"/>
					</div>
				</td>
			</tr>
		</table>
	</div>

	  <?php mysql_close($connection);?>